<template>
  <div style="width: 100%;overflow-y:scroll;height: calc(100vh - 72px)">
    <div class="modern-forms" style="margin-top: 120px">
      <div class="modern-container">
        <form style="text-align: left">
          <fieldset>
            <div class="mdn-group">
              <label class="field-group mdn-upload">
                <input v-model="imgpath" type="text" class="mdn-input" placeholder="no file selected" @click="selectImg" readonly>
                <label class="mdn-label">File Load</label>
                <span class="mdn-bar"></span>
                <span class="mdn-button btn-primary"> Choose Image </span>
              </label>
            </div>
            <template v-if="imgpath">
              <div class="form-row">
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Origin Image</label>
                    <img id="originImg" style="margin-top: 10px"/>
                  </label>
                </div>
                <br/>
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Red</label>
                    <canvas id="redImg" style="margin-top: 10px"/>
                  </label>
                </div>
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Green</label>
                    <canvas id="greenImg" style="margin-top: 10px"/>
                  </label>
                </div>
                <div class="mdn-group">
                  <label class="field-group mdn-upload">
                    <label class="mdn-label">Blue</label>
                    <canvas id="blueImg" style="margin-top: 10px"/>
                  </label>
                </div>
              </div><!-- end form-row -->
            </template>
          </fieldset>
        </form>
      </div><!-- modern-container -->
    </div><!-- modern-forms -->
  </div>
</template>

<script>
import * as d3 from 'd3'
import { sliderBottom } from 'd3-simple-slider';
var mutex = true
export default {
  name: "c6q1",
  data(){
    return{
      imgpath:null,
      processing: false
    }
  },
  methods:{
    selectImg(){
      let input = document.createElement('input');
      input.type = 'file';
      return new Promise(function (resolve) {
        input.onchange = function(ev) {
          resolve(ev.target.files[0])
          return false;
        };
        input.click();
      }).then(file=>{
        this.imgpath = file.path
        const that = this
        return new Promise(function (resolve) {
          var reader = new FileReader();
          reader.onload = function(e){
            // target.result 该属性表示目标对象的DataURL
            let img = document.getElementById('originImg')
            img.src = e.target.result
            img.onload = ()=>resolve(img)
          }
          // 传入一个参数对象即可得到基于该参数对象的文本内容
          reader.readAsDataURL(file);
        })
      }).then(img=>{
        let redcanvas = document.getElementById('redImg')
        let greencanvas = document.getElementById('greenImg')
        let bluecanvas = document.getElementById('blueImg')
        redcanvas.width = img.width;
        redcanvas.height = img.height;
        greencanvas.width = img.width;
        greencanvas.height = img.height;
        bluecanvas.width = img.width;
        bluecanvas.height = img.height;
        let redctx = redcanvas.getContext("2d");
        let greenctx = greencanvas.getContext("2d");
        let bluectx = bluecanvas.getContext("2d");
        redctx.drawImage(img, 0, 0, img.width,img.height);
        greenctx.clearRect(0,0,img.width,img.height);
        bluectx.clearRect(0,0,img.width,img.height);
        let redImgData = redctx.getImageData(0,0,img.width,img.height)
        let greenImgData = greenctx.getImageData(0,0,img.width,img.height)
        let blueImgData = bluectx.getImageData(0,0,img.width,img.height)
        let len = img.width * img.height;
        let R,G,B;
        for(let i = 0;i < len;i++){
          R = redImgData.data[i*4]
          G = redImgData.data[i*4+1]
          B = redImgData.data[i*4+2]
          redImgData.data[i*4] = R;
          redImgData.data[i*4+1] = redImgData.data[i*4+2] = 0;
          greenImgData.data[i*4 + 1] = G;
          blueImgData.data[i*4 + 2] = B;
          redImgData.data[i*4 + 3] = greenImgData.data[i*4 + 3] = blueImgData.data[i*4 + 3] = 255;
        }
        redctx.clearRect(0,0,img.width,img.height);
        redctx.putImageData(redImgData, 0, 0);
        greenctx.putImageData(greenImgData, 0, 0);
        bluectx.putImageData(blueImgData, 0, 0);
      })
    }
  },
  mounted() {
  }
}
</script>

<style scoped>
.axis path,
.axis line{
  fill: none;
  stroke: black;
  shape-rendering: crispEdges;
}

.axis text {
  font-family: sans-serif;
  font-size: 11px;
}

.MyRect {
  fill: steelblue;
}

.MyText {
  fill: white;
  text-anchor: middle;
}

</style>